package cn.iocoder.yudao.module.biz.controller.admin.risk.riskInfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskInfo.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.riskInfo.BizRiskInfoConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskInfo.BizRiskInfoDO;
import cn.iocoder.yudao.module.biz.service.risk.riskInfo.RiskInfoService;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 风险数据")
@RestController
@RequestMapping("/biz/riskInfo")
@Validated
public class BizRiskInfoController {

    @Resource
    private RiskInfoService infoService;
    @Autowired
    private DeptService deptService;

    @PostMapping("/create")
    @ApiOperation("创建风险数据")
    @PreAuthorize("@ss.hasPermission('biz:risk-info:create')")
    public CommonResult<String> createInfo(@Valid @RequestBody BizRiskInfoCreateReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新风险数据")
    @PreAuthorize("@ss.hasPermission('biz:risk-info:update')")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody BizRiskInfoUpdateReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除风险数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-info:delete')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") String id) {
        infoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得风险数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-info:query')")
    public CommonResult<BizRiskInfoRespVO> getInfo(@RequestParam("id") String id) {
        BizRiskInfoDO info = infoService.getInfo(id);
        info.setCompanyName(deptService.getDept(Long.parseLong(info.getCompanyId())).getName());
        BizRiskInfoRespVO vo = BizRiskInfoConvert.INSTANCE.convert(info);
        return success(vo);
    }

    @GetMapping("/list")
    @ApiOperation("获得风险数据列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-info:query')")
    public CommonResult<List<BizRiskInfoRespVO>> getInfoList(@RequestParam("ids") Collection<String> ids) {
        List<BizRiskInfoDO> list = infoService.getInfoList(ids);
        return success(BizRiskInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得风险数据分页")
    @PreAuthorize("@ss.hasPermission('biz:risk-info:query')")
    public CommonResult<PageResult<BizRiskInfoRespVO>> getInfoPage(@Valid BizRiskInfoPageReqVO pageVO) {
        PageResult<BizRiskInfoDO> pageResult = infoService.getInfoPage(pageVO);
        return success(BizRiskInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getRiskInfoPage")
    @ApiOperation("获得风险数据分页")
    @PreAuthorize("@ss.hasPermission('biz:risk-info:query')")
    public PageResult<BizRiskInfoRespVO> getRiskInfoPage(@Valid BizRiskInfoPageReqVO pageVO) {
        PageResult<BizRiskInfoRespVO> result=new PageResult<>();
        List<BizRiskInfoRespVO> list = infoService.getRiskInfoPage(pageVO);
        Integer count= infoService.getRiskInfoCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出风险数据 Excel")
    @PreAuthorize("@ss.hasPermission('biz:risk-info:export')")
    @OperateLog(type = EXPORT)
    public void exportInfoExcel(@Valid BizRiskInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        // 导出 Excel
        List<BizRiskInfoExcelVO> datas = infoService.getAllRiskInfo(exportReqVO);
        ExcelUtils.write(response, "风险数据.xls", "数据", BizRiskInfoExcelVO.class, datas);
    }

    @PostMapping("/import")
    @ApiOperation("导入风险")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class)
    })
    public CommonResult<RiskImportRespVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        return success(infoService.importExcel(file));
    }

    @PostMapping("/getRiskInfoList")
    @ApiOperation("根据责任人获得风险数据分页")
    public PageResult<BizRiskInfoRespVO> getRiskInfoList(@Valid BizRiskInfoPageReqVO pageVO) {
        PageResult<BizRiskInfoRespVO> result=new PageResult<>();
        List<BizRiskInfoRespVO> list = infoService.getRiskInfoList(pageVO);
        Integer count= infoService.getRiskInfoCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }

    @PostMapping(value = "/downRiskTemplate")
    @ApiOperation("下载风险的模板")
    public HttpServletResponse downRiskTemplate(HttpServletResponse response,HttpServletRequest request) {
        try {
            String filename = "风险导入模板.xls";
            InputStream fis = this.getClass().getClassLoader().getResourceAsStream("template/风险导入模板.xls");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            //response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

    @GetMapping("/exportRiskInfoExcel")
    @ApiOperation("导出风险数据 Excel")
    @PreAuthorize("@ss.hasPermission('biz:risk-info:export')")
    @OperateLog(type = EXPORT)
    public void exportRiskInfoExcel(@Valid BizRiskInfoExcelVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        // 导出 Excel
        List<BizRiskInfoExcelVO> datas = infoService.exportRiskInfoExcel(exportReqVO);
        ExcelUtils.write(response, "风险数据.xls", "数据", BizRiskInfoExcelVO.class, datas);
    }
}
